/** **** Objects to represent animations */
var standRight = {name : 'standRight', totalFrames : 1, loop : false, // set to true for animation to loop automatically
	step : 20, // # of frames before animation advances - controls animation
	// speed
	width : 20, height : 33, sY : 66, // Y offset for where animation is located in source file
	sX : 0,
	xOff : -2, yOff : -5
};

var standLeft = {name : 'standLeft', totalFrames : 1, loop : false, step : 20, width : 20, height : 33, sY : 66, sX : 20, xOff : -2, yOff : -5};

var drill = {name : 'drill', totalFrames : 4, loop : true, step : 1, width : 20, height : 33, sY : 99, sX : 0, xOff : -2, yOff : -5};

var block = {name : 'block', totalFrames : 11, loop : false, step : 1, width : 33, height : 33, sY : 132, sX : 0, xOff : -2, yOff : -5};

var walkRight = {name : 'walkRight', totalFrames : 8, loop : true, step : 2, width : 20, height : 33, sY : 0, sX : 0, xOff : -2, yOff : -5};

var walkLeft = {name : 'walkLeft', totalFrames : 8, loop : true, step : 2, width : 20, height : 33, sY : 33, sX : 0, xOff : -2, yOff : -5};

var explode = {name : 'explode', totalFrames : 20, loop : false, step : 2, width : 52, height : 51, sY : 165, sX : 0, xOff : -10, yOff : -25};

var velocity = {x : 0, y : 0, friction : 0.01, acceleration : 0.1, max : 7}

/** * function to create and then return a new Sprite object */
function Sprite() {
	this.canvas = 0; // canvas to hold this sprite - will be drawn to main canvas
	this.ctx = 0; // context for sprite canvas
	this.x = 0; // X position of this sprite
	this.y = 0; // Y position of this sprite
	this.animation = 0; // current animation for this sprite
	this.currentFrame = 0; // current animation frame for this sprite
	this.width = 0;
	this.height = 0;
	this.image = 0; // image that is being drawn to the canvas
	this.currentStep = 0; // number of frames since this sprite's animation
	// was updated
	this.stepOffset = 0; // number of frames to offset so that sprite animations
	// start from the beginning
	this.is_ready = 0; // sprite has finished loading and can be used
	this.initSprite = function(canvas, x, y, width, height, img_file) { // initialize
		// sprite
		this.is_ready = false; // sprite not ready
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		this.canvas = canvas; // canvas is created by Eperiment object and
		// passed to sprite
		this.canvas.setAttribute('width', width);
		this.canvas.setAttribute('height', height);
		// Sprite.ctx = Sprite.canvas.getContext('2d'); //get canvas drawing
		// context
		this.loadImage(img_file); // load image for sprite
		this.animation = standRight; // set initial animation set
	};
	this.loadImage = function(img_file) { // loads image to draw for sprite
		this.image = new Image(); // create new image object
		this.image.onload = this.ready();
		this.image.src = img_file; // load file into image object
	};
	this.ready = function() {
		this.is_ready = true;
	};
	this.drawImage = function(simStepping, ctx, x, y) { // draw image into
		// sprite canvas
		if (this.currentStep == 0) {
			// ensures that the sprite starts at the first frame
			this.stepOffset = (this.animation.totalFrames * this.animation.step) - (simStepping % (this.animation.totalFrames * this.animation.step));
			this.currentStep = 1;
		}
		oldFrame = this.currentFrame;
		if (!this.animation.loop) {
			if (this.currentStep < this.animation.totalFrames)
				this.currentFrame = Math.floor(((simStepping + this.stepOffset) % (this.animation.totalFrames * this.animation.step)) / this.animation.step)
			else
				this.currentFrame = this.animation.totalFrames - 1;
		} else {
			this.currentFrame = Math.floor(((simStepping + this.stepOffset) % (this.animation.totalFrames * this.animation.step)) / this.animation.step)
		}
		if (this.currentFrame != oldFrame)
			this.currentStep += 1;
		// TODO(caskey) I broke non-looping animations
		// Sprite.ctx.clearRect(0,0,Sprite.width,Sprite.height); //clear
		// previous frame
		if (this.is_ready) { // do not draw if sprite is not ready
			// calculate values for sprite based on animation
			var srcX = this.animation.sX + (this.currentFrame * this.animation.width);
			var srcY = this.animation.sY;
			var srcWidth = this.animation.width;
			var srcHeight = this.animation.height;
			var destX = x + this.animation.xOff;
			var destY = y + this.animation.yOff
			ctx.drawImage(this.image, srcX, srcY, srcWidth, srcHeight, destX, destY, srcWidth, srcHeight); // draw image
		}
	};

	this.drill = function() {
		if (this.animation.name != drill.name) {
			this.animation = drill;
			this.currentFrame = 0;
			this.currentStep = 0;
		}
	};
	this.block = function() {
		if (this.animation.name != block.name) {
			this.animation = block;
			this.currentFrame = 0;
			this.currentStep = 0;
		}
	};
	this.goLeft = function() {
		if (this.animation.name != walkLeft.name) {
			this.animation = walkLeft;
			this.currentFrame = 0;
			this.currentStep = 0;
		}
	};
	this.goRight = function() {
		if (this.animation.name != walkRight.name) {
			this.animation = walkRight;
			this.currentFrame = 0;
			this.currentStep = 0;
		}
	};
	this.explode = function() {
		if (this.animation.name != explode.name) {
			this.animation = explode;
			this.currentFrame = 0;
			this.currentStep = 0;
		}
	};
	this.stopMovement = function() {
		if (this.animation.name == walkLeft.name) {
			this.animation = standLeft;
			this.currentFrame = 0;
			this.currentStep = 0;
		} else if (this.animation.name == walkRight.name) {
			this.animation = standRight;
			this.currentFrame = 0;
			this.currentStep = 0;
		} else {
			this.animation = standRight;
			this.currentFrame = 0;
			this.currentStep = 0;
		}
	};
	this.initSprite(document.createElement('canvas'), 490, 265, 33, 33, BASE_URL + 'images/sprite.png');
};
